<template>
  <c-list
    v-if="fileItems.length>0"
    :data-source="fileItems"
    layout="horizontal"
    :split="false"
    :gutter="10"
    class="file-list-container"
    item-width="300px"
    item-layout="vertical"
    item-padding="5px"
    :item-border="true">
    <template #item="{item}">
      <c-item :title-noraml="true">
        <template #title>
          <fileViewer :type="item.fileType" :url="item.fileUuid" :title="item.originalFileName+'.'+item.fileExt">
            <div class="file-name" :style="{width:(remove?'183px':'220px')}"> {{ item.originalFileName+'.'+item.fileExt }}</div>
          </fileViewer>
        </template>
        <template #avatar>
          <div class="file-icon">
            <el-image v-if="item.fileType===1" :src="common.getFileView(item.fileUuid)" style="width: 40px" fit="contain" lazy />
            <FileTypeSvgIcon v-else :type="item.fileType" />
          </div>
        </template>
        <template #desc>
          {{ item.fileSize | formatFileSize }}
        </template>
        <template v-if="remove" #extra>
          <div class="remove-icon">
            <i class="el-icon-close" @click.stop="removeFile(item.fileUuid)" />
          </div>
        </template>
      </c-item>
    </template>
  </c-list>
</template>
<script>
export default {
  name: 'FileItem',
  props: {
    files: {
      type: Array,
      default() {
        return []
      }
    },
    remove: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      fileItems: []
    }
  },
  watch: {
    files: function(val) {
      this.fetchFile()
    }
  },
  created() {
    this.fetchFile()
  },
  methods: {
    fetchFile() {
      if (this.files.length > 0) {
        if (this.files[0].length !== 0) {
          this.$store.dispatch('listViewFileResourcesByUUID', { uuids: this.files.join(',') }).then((res) => {
            this.fileItems = res
          })
        } else {
          this.fileItems = []
        }
      } else {
        this.fileItems = []
      }
    },
    removeFile(uuid) {
      if (!uuid) {
        this.$modal.msgWarning('缺少文件标识')
        return
      }
      this.$emit('remove', uuid)
    }
  }
}
</script>

<style lang="scss" scoped>
.file-list-container{
  margin-top: 15px !important;
  overflow: auto;
  .file-icon{
    display: flex;
    flex-direction: column;
    justify-content: center;
    height: 50px;
    align-items: center;
    .svg-icon{
      width: 2.9em !important;
      height: 2.9em !important;
    }
  }
  .file-name{
    color: #404040;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
  }
  .remove-icon{
    width: 20px;
    position: absolute;
    top: 0px;
    right: 0;
    text-align: center;
    i{
      cursor: pointer;
    }
  }
}
</style>
